package com.hxzy.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.controller.admin.login.req.AdminLoginReq;
import com.hxzy.controller.admin.login.resp.AdminLoginResp;
import com.hxzy.controller.admin.merchandiser.req.MerchandiserReq;
import com.hxzy.controller.admin.merchandiser.req.ProcessReq;
import com.hxzy.controller.admin.merchandiser.resp.MerchandiserResp;
import com.hxzy.controller.front.merchandiser.req.MerchandiserIdReq;
import com.hxzy.entity.Employee;
import com.hxzy.entity.EnterpriseProcess;
import com.hxzy.entity.Merchandiser;
import com.hxzy.mapper.EnterpriseProcessMapper;
import com.hxzy.service.EnterpriseProcessService;
import com.hxzy.service.MerchandiserService;
import com.hxzy.mapper.MerchandiserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
* @author Administrator
* @description 针对表【merchandiser(商家信息)】的数据库操作Service实现
* @createDate 2023-06-28 16:38:44
*/
@Service
public class MerchandiserServiceImpl extends ServiceImpl<MerchandiserMapper, Merchandiser>
    implements MerchandiserService{

    @Autowired
    private MerchandiserMapper merchandiserMapper;

    @Autowired
    private EnterpriseProcessMapper enterpriseProcessMapper;

    /**
     * 分页查询
     * @param req
     * @return
     */
    @Override
    public PageInfo<MerchandiserResp> search(MerchandiserReq req) {

        return PageHelper.startPage(req.getPage(),req.getSize())
                .doSelectPageInfo(()-> {
                    merchandiserMapper.search(req);
                });
    }

    /**
     * 审核2个表操作
     * 1）、更新原来商家信息表       update
     * 2)、EnterpriseProcess新增一笔数据   insert
     * @param req
     * @return
     */
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public boolean process(ProcessReq req) {


        Merchandiser merchandiser=new Merchandiser();
        merchandiser.setId(req.getTenantId());
        merchandiser.setStatus(req.getStatus());
        boolean success = this.updateById(merchandiser);
        if(success){
            //插入审核数据
            EnterpriseProcess  process=new EnterpriseProcess();
            process.setTenantId(req.getTenantId());
            process.setDescription(req.getDescription());
            process.setStatus(req.getStatus());
            success=this.enterpriseProcessMapper.insert(process)>0;
        }
        return success;
    }

    /**
     * 前端分页查询
     * @param req
     * @return
     */
    @Override
    public PageInfo<com.hxzy.controller.front.merchandiser.resp.MerchandiserResp> frontSearch(com.hxzy.controller.front.merchandiser.req.MerchandiserReq req) {
        return PageHelper.startPage(req.getPage(),req.getSize())
                .doSelectPageInfo(()-> {
                    merchandiserMapper.frontSearch(req);
                });
    }

    @Override
    public com.hxzy.controller.front.merchandiser.resp.MerchandiserResp findById(MerchandiserIdReq req) {
        return merchandiserMapper.frontFindById(req);
    }

    @Override
    public AdminLoginResp login(AdminLoginReq req) {
        //支持 Lambda 形式调用：通过 Lambda 表达式，方便的编写各类查询条件，无需再担心字段写错
        LambdaQueryWrapper<Merchandiser> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Merchandiser::getLoginName, req.getLoginName());
        Merchandiser db = this.getOne(queryWrapper);
        if(db==null){
            throw new ServiceException(AckCode.USER_PASSWORD_NOT_FOUND);
        }
        //md5增强版 bcrypt --> Hutool工具类实现的 密码
        boolean correct= BCrypt.checkpw(req.getLoginPwd(), db.getLoginPwd());
        if(!correct){
            throw new ServiceException(AckCode.USER_PASSWORD_NOT_FOUND);
        }
        //状态
        if(!db.getStatus().equals("1")){
            throw new ServiceException(AckCode.ENTERPRISE_NOT_VALIDATED);
        }
        //转换自定义对象
        AdminLoginResp resp=new AdminLoginResp();
        resp.setId(db.getId());
        resp.setLoginName(db.getLoginName());
        resp.setName(db.getName());
        resp.setAvatar(db.getPicture());
        resp.setType(2);
        return resp;
    }
}




